package com.example.a313ldy.yuejian_17.Model;


import com.example.a313ldy.yuejian_17.Bean.StringBean;
import com.example.a313ldy.yuejian_17.Interface.LoginAndRegisterFace;
import com.example.a313ldy.yuejian_17.Listener.UserListener;
import com.example.a313ldy.yuejian_17.Service.LoginService;
import com.example.a313ldy.yuejian_17.Service.RegisterService;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.a313ldy.yuejian_17.Gloablecalss.Yuejian.URL;


/**
 * Created by LDY on 2017/9/28.
 */

public class UserModel implements LoginAndRegisterFace {

    private RegisterService  registerService;
    private LoginService loginService;
    private Retrofit retrofit;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址

    public UserModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())//解析方式
                .build();
    }


    @Override
    public void getLogin(String tele, String pass, final UserListener userListener) {
        loginService = retrofit.create(LoginService.class);
        Call<StringBean> call = loginService.LoginImpl(tele, pass);

        call.enqueue(new Callback<StringBean>() {
            @Override
            public void onResponse(Call<StringBean> call, Response<StringBean> response) {
                if (response.isSuccessful() && response.body().getError() == null)
                    userListener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<StringBean> call, Throwable t) {
                userListener.onFail("登录失败!");
            }
        });

    }

    @Override
    public void getRegister (String tele, String password,String CN_name,final UserListener userListener){
        registerService = retrofit.create(RegisterService.class);
            Call<StringBean> call = registerService.RegImpl(tele, password);
            call.enqueue(new Callback<StringBean>() {
                @Override
                public void onResponse(Call<StringBean> call, Response<StringBean> response) {
                    if (response.isSuccessful() && response.body().getError() == null) {
                        userListener.onResponse(response.body());
                    }
                }

                @Override
                public void onFailure(Call<StringBean> call, Throwable t) {
                    userListener.onFail("注册失败!");
                }
            });

        }
    }





